package com.project.service;

import com.project.bean.CutPageBean;
import com.project.bean.UserBean;
import com.project.dao.IUserDao;
import com.project.dao.impl.UserDaoImpl;
import com.project.server.Request;
import com.project.server.Response;
import com.project.server.Servlet;
import com.project.server.WebServlet;

/**
 * 分页显示业务组件
 */
@WebServlet("cutAll")
public class CutPageService  implements Servlet {
    private IUserDao userDao = new UserDaoImpl();

    public void service(Request request, Response response){
        //得到客户端的表单数据
        String pg = request.getParameter("pageNO");
        //得到页码
        int pageNO = pg == null ?1 : Integer.parseInt(pg);

        //调用持久方法，得到结果
        CutPageBean<UserBean> cutBean = userDao.cutAll(pageNO);

        String str = "<table border='1' width='60%'>";
        str += "<thead><tr><th>用户名</th><th>密码</th><th>生日</th><th>工资</th></tr></thead>";
        str += "<tbody>";
        for(UserBean user : cutBean.getList()){
            String s= "<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>";
            str += String.format(s,user.getName(),user.getPwd(),user.getBirthday(),user.getMoney());
        }

        str += "</tbody></table>";

        for(int i=1;i<= cutBean.getTotalPage();i++){
            String s = "<a style='margin-left:20px' href='cutAll?pageNO=%s'>%s</a>";
            str += String.format(s,i,i);
        }

        response.sendMessage(str);

    }
}
